package com.fwj.abstractexample;

import java.util.*;

/**
 * @author stone.feng
 * @since 2019-01-28
 */
public class CollectionTest {

    public static void main(String[] args) {

        final String A = "ab";

        final String B = "cd";

        String s = A + B;

        String t = "abcd";

        if (s == t) {
            System.out.println("同一个对象");
        } else {
            System.out.println("不是同一个对象");
        }

        // arrayListForeachTest();

        // hashMapTest();

        linkedListTest();

        Map a = new HashMap();

        // setTest();
    }

    private static void setTest() {
        Set<String> set = new HashSet<>();
        set.add(null);
        set.add("1");
        set.add("22");
        set.add(null);
        System.out.println(set);
    }

    private static void hashMapTest() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put(null, null);
        hashMap.put("ds", null);
        hashMap.put(null, "2");
        hashMap.put(null, null);
        System.out.println(hashMap.toString());
    }

    private static void arrayListForeachTest() {
        // 反例  不能在foreach循环里进行元素的remove add
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        for (String item : list) {
            System.out.println(item);
            if ("1".equals(item)) {
                list.remove(1);
            }
        }
        System.out.println(list);
    }

    private static void linkedListTest() {
        List<String> list = new LinkedList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("3");
        System.out.println(list);
        System.out.println("链表的第一个元素是 : " + ((LinkedList<String>) list).getFirst());
        System.out.println("链表最后一个元素是 : " + ((LinkedList<String>) list).getLast());

        list.add("first");
        list.add("second");
        list.add("third");
        System.out.println(list);

        ((LinkedList<String>) list).addFirst("addFirst");
        System.out.println(list);

        ((LinkedList<String>) list).addLast("addLast");
        System.out.println(list);

        list.add(2, "addByIndex");
        System.out.println(list);

        list.remove("second");
        System.out.println(list);

    }


}